package com.travel.server.manager.impl.dal.data;

import com.travel.common.util.CollectionUtil;
import com.travel.server.manager.dao.ProjectConsultDao;
import com.travel.server.manager.model.ProjectConsult;
import com.travel.server.manager.model.ProjectConsultExample;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/30 12:44
 * @Desc:
 */
@Component
public class ConsultDal {

    @Autowired
    private ProjectConsultDao dao;

    public ProjectConsult selectProjectConsult(ConsultParam param){
        ProjectConsultExample example = makeExample(param);
        List<ProjectConsult> list = dao.selectByExample(example);
        return CollectionUtil.getFirst(list);
    }

    public void insertOrUpdate(ProjectConsult projectConsult){
        if(projectConsult.getId() == null){
            dao.insertSelective(projectConsult);
        }else{
            dao.updateByPrimaryKey(projectConsult);
        }
    }

    private ProjectConsultExample makeExample(ConsultParam param){
        ProjectConsultExample example = new ProjectConsultExample();
        ProjectConsultExample.Criteria criteria = example.or();
        if(param.getProjectId() != null){
            criteria.andProjectIdEqualTo(param.getProjectId());
        }
        if(param.getDataDate() != null){
            criteria.andDataDateEqualTo(param.getDataDate());
        }
        return example;
    }

    @Data
    public static class ConsultParam{
        private Integer projectId;

        private Date dataDate;
    }
}
